package alloy.gui;
import java.awt.Toolkit;

import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleContext;

public class ConsoleDocument extends DefaultStyledDocument {
    private int _wOff;

    public ConsoleDocument() {
	super();
	_wOff = 0;

    }

    public ConsoleDocument(AbstractDocument.Content c, StyleContext styles) {
	super(c, styles);
	_wOff = 0;
	
    }

    public ConsoleDocument(StyleContext styles) {
	super(styles);
	_wOff = 0;

    } 


    /**
     * Makes the document contents before a given offset
     * uneditable by the user.
     * @param offset marks the position in the document
     * from which the user is allowed to edit
     **/
    public void readOnlyBeforeOffset(int offset) {
	_wOff = offset;
    }

    /**
     * @return the offset in the document from which the
     * user is allowed to make edits
     **/
    public int getWritableOffset() {
	return _wOff;
    }

    /**
     * Makes the document contents before the current offset
     * uneditable by the user.
     **/
    public void readOnlyBeforeCurrent() {
	readOnlyBeforeOffset(getEndPosition().getOffset() - 1);
    }

    public void insertString(int offs, String str, AttributeSet a) 
        throws BadLocationException {

	// no insertion in the read-only portion
	if (offs < _wOff) {
	    Toolkit.getDefaultToolkit().beep();
	}
	else
	    super.insertString(offs, str, a);

    }

    public void remove(int offs, int len) throws BadLocationException {
	
	// no removal in the read-only portion
	if (offs < _wOff)
	    Toolkit.getDefaultToolkit().beep();
	else
	    super.remove(offs, len);
        
    }
}
